package com.spark.rectify

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import com.spark.common.BaseActivity
import com.spark.common.helper.MutableStatePagerAdapter
import com.spark.common.helper.Picker
import kotlinx.android.synthetic.main.act_rect_input.*
import me.yokeyword.fragmentation.SupportFragment

class RectInputActivity : BaseActivity() {
    private lateinit var inputAdapter: MutableStatePagerAdapter

    companion object {
        fun actionStart(host: Activity) {
            val intent = Intent(host, RectInputActivity::class.java)

            host.startActivity(intent)
        }
    }

    override fun contentResId(): Int {
        return R.layout.act_rect_input
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        iv_back.setOnClickListener { finish() }
        tv_title.text = "纠违录入"

        var tabTitles = mutableListOf("纠违信息", "处罚单预览")
        var frags = mutableListOf<Fragment>(
                RectInfoFragment.newInstance(),
                PunishOrderPreviewFragment.newInstance()
        )

        tabTitles.forEach {
            tabs.addTab(tabs.newTab().setText(it))
        }
        inputAdapter = object : MutableStatePagerAdapter(supportFragmentManager) {
            override fun getCount(): Int {
                return tabTitles.size
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return tabTitles[position]
            }
        }
        inputAdapter.adds(frags)
        pager.apply {
            adapter = inputAdapter
            canScroll = false
        }
        tabs.setupWithViewPager(pager)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Picker.onActivityResulted(this, requestCode, resultCode, data)
        inputAdapter.pageList.forEach {
            (it.fragment as SupportFragment).onFragmentResult(requestCode, resultCode, data?.extras)
        }
    }
}